<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

/**
 * Ajax action to save free content into database
 */
class m_website_actions_AjaxSaveFreeContentAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		$values = $this->getRequest()->getParameterValues("form");
		
		$documentId = $values["pageId"];
		$documentLang = $values["pageLang"];
		$blockId = $values["blockId"];

		$right = $this->getUserContext()->hasRight("website_AjaxValidatePage");
		$tempDocService = m_backoffice_services_TempDocService::getInstance();		
		
		if($tempDocService->hasTemporary($documentId, $documentLang))
		{
			$page = $tempDocService->getTemporaryDocument($documentId, $documentLang);
		}
		else
		{
			$page = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $documentId, $documentLang);			
		}		
		
		$block = $page->getBlockById($blockId);
		
		if(is_null($block['contentId']) || $block['contentId'] == "")
		{
			$doc = m_website_documents_FreeContent::createNew();
			
			$doc->setAuthor($this->getUser()->getLogin())
				->setPublicationstatus("DRAFT")
				->setLang($documentLang)
				->setContent($values["content"])
				->save(false);
			
			$values['contentId'] = $doc->getUniqueId();
			$values["revision"] = $doc->getLastRevisionNumber();

			if($values["withValidation"] == "true" && $right == true)
			{
				if($tempDocService->hasTemporary($documentId, $documentLang))
				{
					$tempDocService->deleteTempDocument($documentId, $documentLang);
				}
				
				$doc->save();
			}
			else
			{
				$tempDocService->saveTemporaryDocument($doc);					
			}
			
			$this->fireInfo($values);
		}
		else
		{
			if($tempDocService->hasTemporary($block['contentId'], $documentLang))
			{
				$doc = $tempDocService->getTemporaryDocument($block['contentId'], $documentLang);
			}
			else
			{
				$doc = f_document_Provider::getInstance()->getByModelAndUniqueId('website/FreeContent', $block['contentId'], $documentLang);
			}

            $content = m_website_services_FreeContentService::getInstance()->parseImages($values["content"]);

			$doc->setAuthor($this->getUser()->getLogin())->setContent($content);
			
			if($values["withValidation"] == "true" && $right == true)
			{
				if($tempDocService->hasTemporary($documentId, $documentLang))
				{
					$tempDocService->deleteTempDocument($documentId, $documentLang);
				}
				
				$doc->save();
			}
			else
			{
				$tempDocService->saveTemporaryDocument($doc);
			}
			
			$values['contentId'] = $doc->getUniqueId();
			$values["revision"] = $doc->getLastRevisionNumber();
			
			$this->fireInfo($values);
		}				
		
		foreach($values as $key => $value)
		{
			if($key != "pageId" && $key != "pageLang" && $key != "blockId" && $key != "content")
			{
				$block[$key] = stripslashes($value);
			}
		}
		
		$page->setBlockParamsById($blockId, $block);
		
		if($values["withValidation"] == "true" && $right == true)
		{
			if($tempDocService->hasTemporary($page->getUniqueId(), $documentLang))
			{
				$tempDocService->deleteTempDocument($page->getUniqueId(), $documentLang);
			}
			
			$page->save(false);
		}
		else
		{
			$tempDocService->saveTemporaryDocument($page, $documentLang);					
		}
		
		echo '<div id="formsuccess" style="display:none">blockEditRefresh</div>';
	}
}